import os
from gravityassist.lyrics import transcript_audio
from gravityassist.config import get_tool_cache, put_tool_cache
from gravityassist.musiclib import get_music_list
from hashlib import sha256

def music_lyrics(path: str):
    if not os.path.exists(path):
        if path.endswith('.mp3') or path.endswith('.mp4'):
            path = path[:-4]
        for music in get_music_list():
            if music["name"] == path:
                path = music["path"]
                break
    if not os.path.exists(path):
        return {'success': False, 'message': 'File does not exist, please play the music then use current_music for getting accurate music path'}
    cache_key = "music_lyrics-" + sha256(path.encode()).hexdigest()
    text = get_tool_cache(cache_key)
    if text is None:
        text = transcript_audio(path)
        put_tool_cache(cache_key, text)
    assert isinstance(text, str), type(text)
    return {'success': True, 'lyrics': text}
